package com.bite.blog.service;

import com.bite.blog.mapper.BlogMapper;
import com.bite.blog.mapper.UserMapper;
import com.bite.blog.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BlogMapper blogMapper;

    public User login(String userName) {
        return userMapper.queryByName(userName);
    }


    public User getUserInfoById(Integer id) {
        return userMapper.queryById(id);
    }

    public User getAuthorByBlogId(Integer blogId) {
        // 1. 先根据 blogId 找到 userId
        Integer userId = blogMapper.queryById(blogId).getUserId();
        if (userId == null || userId < 0) {
            log.error("blogId为" + blogId + "的文章不合法：userId=" + userId);
            return null;
        }
        // 2. 在根据 userId 找到 作者信息
        return userMapper.queryById(userId);
    }
}
